using System;
using System.Collections.Generic;
using System.Text;
using CodePlex.Resourceful;
using CodePlex.Resourceful.Amazon.S3;

namespace CodePlex.SpaceBlock.FS
{
    public class BaseFileSystem
    {
        protected static class BaseMetadataKeys
        {
            public static readonly string Path = "Path";
            public static readonly string Type = "Type";
        }

        protected void CheckNull(Path path)
        {
            if (path == null)
            {
                throw CodePlexException.Format("Path cannot be null.");
            }
        }

        protected void CheckFolder(Path path)
        {
            if (!path.IsFolder)
            {
                throw CodePlexException.Format("Path must be a folder: '{0}'.", path);
            }
        }

        protected void CheckFile(Path path)
        {
            if (path.IsFolder)
            {
                throw CodePlexException.Format("Path must be a file: '{0}'.", path);
            }
        }


        protected void AddMetadata(Metadata metadata, string key, string value)
        {
            string category = this.GetMetadataCategory(key);
            metadata.Set(true, category, key, this.GetMetadataDescription(key, category), value);
        }

        virtual protected string GetMetadataCategory(string key)
        {
            if (Utils.In(key,
               BaseMetadataKeys.Path,
               BaseMetadataKeys.Type
               ))
            {
                return "(standard)";
            }

            return "Other";

        }


      

        virtual protected string GetMetadataDescription(string key, string category)
        {
            NameValuePairs descriptions = SpaceBlockUtils.HttpHeaderDescriptions();

            descriptions[BaseMetadataKeys.Path] = "SpaceBlock standard object path.";
            descriptions[BaseMetadataKeys.Type] = "SpaceBlock standard object type.";

            
            return descriptions.GetIfExists(key);
        }

        protected string ComputeSizeString(long length)
        {
            return string.Format("{0:#,0}  ({1})", length, Utils.ComputeSizeString(length));
        }


        
        protected class PathItem
        {
            private readonly IFileSystem _fileSystem;
            private readonly Path _path;
            private readonly DateTime? _lastModified;


            public PathItem(IFileSystem fileSystem, Path path, DateTime? lastModified)
            {
                _fileSystem = fileSystem;
                _path = path;
                _lastModified = lastModified;
            }

            public IFileSystem FileSystem
            {
                get { return _fileSystem; }
            }

            public Path Path
            {
                get { return _path; }
            }

            public DateTime? LastModified
            {
                get { return _lastModified; }
            }
        }

        protected class FolderItem : PathItem, IFolderItem
        {
            public FolderItem(IFileSystem fileSystem, Path path, DateTime? lastModified)
                : base(fileSystem, path, lastModified) { }
        }
    }
}
